# -*- coding: utf-8 -*-
'''
Created on 2013-5-31
主窗口
@author: 不懂d叛逆
'''
import re
import ccjhConnect, ccjhMessageHandle, ccjhCommmandHandle, ccjhObj, ccjhContainer, ccjhUser, ccjhMan

class pyccjh():
	def __init__(self, Server, Port, IP, ID, Password):
		self.server = Server
		self.port = Port
		self.ip = IP
		self.id = ID
		self.password = Password
		self.__msgHandle = ccjhMessageHandle.messageHandle(self)
		self.connect = ccjhConnect.ccjhConnect(self.__msgHandle.notify, self.server, self.port, self.id, self.password)

		self.__cmdHandle = ccjhCommmandHandle.cmdHandle(self)
		self.Target = 0
		self.where = ccjhObj.ccjhObj("")
		self.doors = []
		self.mans = ccjhContainer.ccjhContainer()
		self.user = ccjhUser.ccjhUser()
		return

	def __del__(self):
		self.connect.Close(False)
		return

	def DisConnect(self):
		self.connect.Close(False)
		return

	def Connect(self):
		self.connect.Connect()
		return

	def AddText(self, string):
		string = re.sub("\$[A-Z]{3}", "", string)  # 删除颜色代码
		print(string)
		return

	def Cmd(self, command):
		self.__cmdHandle.cmd(command)
		return

	def TargetT(self, string):
		if string == self.user.get("o"):  # ID
			self.Target = self.user.LongID
		else:
			man = self.user.FindByID(string)
			if isinstance(man, ccjhMan.ccjhMan):
				self.Target = man.LongID
			else:
				man = self.mans.FindByID(string)
				if isinstance(man, ccjhMan.ccjhMan):
					self.Target = man.LongID
				elif string == "0":
					self.Target = 0
				else:
					self.AddText("目标不存在！")
		return
